import logging
import smtplib
from email.mime.text import MIMEText
from email.header import Header

from fastapi import Depends

from app.config import config
from app.dependencies import Dependencies


class EmailServer:
    SMTP_SERVER = config.email.smtp
    Sender = config.email.sender
    Password = config.email.password
    Port = config.email.port
    SSL = config.email.ssl

    @classmethod
    def send_email(cls, receivers, message, logger):
        smtp_obj = smtplib.SMTP(cls.SMTP_SERVER, cls.Port)
        try:

            email_message = MIMEText(message, 'plain', 'utf-8')
            email_message['From'] = Header(cls.Sender)
            email_message['To'] = Header(','.join(receivers))
            email_message['Subject'] = Header('Python邮件测试', 'utf-8')
            smtp_obj.starttls()
            smtp_obj.login(cls.Sender, cls.Password)
            smtp_obj.sendmail(cls.Sender, receivers, email_message.as_string())
        except Exception as e:
            logger.error(f"邮件发送错误{receivers}"+ str(e))

        finally:
            smtp_obj.quit()

